package com.classroompie.classroompieserver.core;


import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
 * @author jiahao
 * @since 2024/6/20 周四
 */
import java.security.SecureRandom;
import java.util.HashSet;
import java.util.Set;

@Component
public class UniqueStringGenerator {
    private static final int STRING_LENGTH = 6;
    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static final SecureRandom random = new SecureRandom();
    private static final Set<String> generatedStrings = new HashSet<>();

    public static String generateUniqueString() {
        StringBuilder sb;
        String newString;

        do {
            sb = new StringBuilder(STRING_LENGTH);
            for (int i = 0; i < STRING_LENGTH; i++) {
                int index = random.nextInt(CHARACTERS.length());
                sb.append(CHARACTERS.charAt(index));
            }
            newString = sb.toString();
        } while (generatedStrings.contains(newString));

        generatedStrings.add(newString);
        return newString;
    }

}

